package com.example.expandablelistviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> groupList;
    private List<List<String>> itemList;

    public MyAdapter(Context context,List<String> groupList,List<List<String>> itemList){
        this.context = context;
        this.groupList = groupList;
        this.itemList = itemList;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return itemList.get(groupPosition).size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        Hoder hoder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.group_layout,null);
            hoder = new Hoder();
            hoder.name = convertView.findViewById(R.id.group_text);
            hoder.img = convertView.findViewById(R.id.group_img);
            convertView.setTag(hoder);
        }else{
            hoder = (Hoder)convertView.getTag();
        }

        if(isExpanded){
            hoder.img.setImageResource(R.drawable.open);
        }else{
            hoder.img.setImageResource(R.drawable.close);
        }

        hoder.name.setText(groupList.get(groupPosition));

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        Hoder hoder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_layout,null);
            hoder = new Hoder();
            hoder.name = convertView.findViewById(R.id.item_name);
            hoder.img = convertView.findViewById(R.id.item_img);
            convertView.setTag(hoder);
        }else{
            hoder = (Hoder)convertView.getTag();
        }

        hoder.name.setText(itemList.get(groupPosition).get(childPosition));
        hoder.img.setBackgroundResource(R.drawable.item);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

class Hoder {
    TextView name;
    ImageView img;
}
